package com.twentytwok.hospitalsystem22K.controller;

import com.twentytwok.hospitalsystem22K.bo.DoctorBo;
import com.twentytwok.hospitalsystem22K.common.PageVo;
import com.twentytwok.hospitalsystem22K.common.R;
import com.twentytwok.hospitalsystem22K.common.R2;
import com.twentytwok.hospitalsystem22K.pojo.DoctorUser;
import com.twentytwok.hospitalsystem22K.service.DoctorUserService;
import com.twentytwok.hospitalsystem22K.util.QiNiuUtils2;
import com.twentytwok.hospitalsystem22K.vo.DoctorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@RestController
@CrossOrigin("*")
@RequestMapping("/doctor")
public class DoctorUserController {

    @Autowired
    private DoctorUserService doctorUserService;

    @RequestMapping("/pageData")
    public PageVo<DoctorUser> getPageData(@RequestParam Map<String, Object> params) {

        return doctorUserService.getPageData(params);
    }

    @RequestMapping("/update")
    public R2 update(DoctorUser doctorUser) {
        return doctorUserService.update(doctorUser);
    }


    @RequestMapping("/delete")
    public R2 delete(Integer id){
        return doctorUserService.delete(id);
    }

    @RequestMapping("/add")
    public R2 add(DoctorUser doctorUser) {
        return doctorUserService.add(doctorUser);
    }

    @RequestMapping("/upload")
    public R2 uploadPic(MultipartFile file, HttpServletRequest request) throws IOException {

        String filename = file.getOriginalFilename();

        String suffix = filename.substring(filename.lastIndexOf("."));


        String fileName = UUID.randomUUID().toString()+suffix;

        byte[] bytes = file.getBytes();

        QiNiuUtils2.uploadByQiniu(bytes, fileName);


        return R2.ok(200,"上传成功","http://r234r512r.hn-bkt.clouddn.com/"+fileName);

    }

    /**
     * 根据指定的部门id和时间段id获取对应的医生列表
     *
     * @author Yuanao
     * @date 2021年11月12日 11:59:55
     *
     * @param departmentId 医生id
     * @param timeQuantumId 时间段id
     * @return json
     */
    @RequestMapping("/getDoctorByDepartmentIdAndTimeQuantumId")
    public R getDoctorByDepartmentIdAndTimeQuantumId(@RequestParam Integer departmentId,
                                                     @RequestParam Integer timeQuantumId,
                                                     @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate date) {
        return doctorUserService.getDoctorByDepartmentIdAndTimeQuantumId(departmentId,date,timeQuantumId);
    }

    @RequestMapping("/bar")
    public DoctorVo findDoctorVo() {
        return doctorUserService.selectDepartmentVo();
    }

    @RequestMapping("/pies")
    public List<DoctorBo> findDoctorBo() {
        return doctorUserService.selectDepartmentBo();
    }

}
